home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / web / spiderweb / tools / depend.web < prev    next >
Text File  |  1988-07-13  |  3KB  |  129 lines

  1. \def\title{Finding dependencies in a {\tt WEB}  file}
  2. @*Introduction.
  3. Often we want to know what files another {\tt WEB}  file depends on, for
  4. use in ``{\tt make depend}.''
  5. This program reads a {\tt WEB} file, looking for |"@i"| at the 
  6. beginning of a line, and writes
  7. the names of all of the include files on standard output.
  8. It searches the paths set by the |WEBPATH| environment variable.
  9.  
  10. Usage is:
  11. $${\tt depend [-a<at-sign>] [-I<directory>] [files...]}$$
  12. If no at sign is specified, we use |'@@'|.
  13. If no files are specified, we read from standard input.
  14. In no case do we write the name of the file given on the command line.
  15.  
  16. @ The main program just processes the arguments.
  17. |switchar(c)| tells whether |c| introduces an option.
  18. @d switchar(c) = ((c)=='-')
  19. @u
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. @i pathopen.h
  23. static char at_sign = '@@';
  24.  
  25. main(argc,argv) 
  26.     int argc; char **argv;
  27. {
  28.         FILE *fp;
  29.  
  30.     @<Read options and set |argc|, |argv| to remaining args@>@;
  31.  
  32.         pathaddpath(getenv("WEBPATH"),':');
  33.  
  34.     if (argc==0)
  35.         depend(stdin);
  36.     else while (argc-->0)
  37.         if ((fp=pathopen(*argv++))!=NULL)
  38.             depend(fp);
  39.         else {
  40.             fprintf(stderr,"Can't find %s on search path\n",*--argv);
  41.             exit(1);
  42.         }
  43. }
  44.  
  45. @ @<Read options...@>=
  46. while (--argc>0) 
  47.     if (switchar(**++argv))
  48.         switch(*++(*argv)) {
  49.             case 'a': 
  50.                 ++*argv;
  51.                 @<Set |at_sign| according to |**argv|@>@;
  52.                 break;
  53.             case 'I':
  54.                         pathaddname(++(*argv));
  55.                         break;
  56.                        default:
  57.                             fprintf(stderr,"Unknown option -%c\n", **argv);
  58.                             break;
  59.                 }
  60.     else break;
  61.  
  62. @ Since {\tt \#} is hard to write in {\tt Makefile}, we
  63. let {\tt s} stand for {\tt \#}.
  64. @<Set |at_sign| according to |**argv|@>=
  65. at_sign = **argv;
  66. if (at_sign=='s') at_sign='#';
  67.  
  68. @ All the real work is done by the recursive function |depend|, which
  69. searches for {\tt @@i} and prints its findings on |stdout|.
  70. @u
  71. depend (fp)
  72.     FILE *fp;
  73. {
  74.     int c;
  75.     char filename[128], *fn;
  76.     FILE *fp2;
  77.     
  78.     c='\n'; /* begin like at the end of a line */
  79.     do {
  80.         if (c=='\n') {
  81.               end_line:
  82.             if ((c=getc(fp))==EOF) break;
  83.             if (c==at_sign) {
  84.                 if ((c=getc(fp))==EOF) break;
  85.                 if (c=='i' || c=='I') { /* live one */
  86.                     @<Get name, print, and call |depend|@>@;
  87.                 } else if (c=='\n') goto end_line;
  88.             } else if (c=='\n') goto end_line;
  89.         }
  90.     } while ((c=getc(fp))!=EOF);
  91.     fclose(fp);
  92. }
  93.  
  94. @ @<Get name, print, and call |depend|@>=
  95. while ((c=getc(fp))!=EOF)
  96.     if (!isspace(c)) break;
  97. if (c==EOF) break;
  98. fn=filename;
  99. do {
  100.     if (isspace(c)) break;
  101.     else {
  102.         *fn++=c;
  103.         putchar(c);
  104.     }
  105. } while ((c=getc(fp))!=EOF);
  106. *fn='\0';
  107. putchar('\n');
  108. if ((fp2=pathopen(filename))!=NULL)
  109.     depend(fp2);
  110. else {
  111.     fprintf(stderr,"Can't find %s on search path\n",filename);
  112.     exit(1);
  113. }
  114. if (c==EOF) break;
  115. if (c=='\n') goto end_line;
  116. /* N.B. to reiterate |isspace(c)|, so |c!=at_sign| */
  117.  
  118. @ Here's what |pathopen| and friends call in case of overflow.
  119. @u
  120. overflow(s)
  121.     char *s;
  122. {
  123.     fprintf(stderr,"Sorry, capacity exceeded: %s\n",s);
  124.     exit(1);
  125. }
  126.  
  127. @*Index.
  128.  
  129.